/*************************************************************************
 * gOkey - A tile based game for Gnome.                                  *
 * Copyright (C) 2008  Onur Aslan  <onuraslan@gmail.com>                 *
 *                                                                       *
 * This program is free software: you can redistribute it and/or modify  *
 * it under the terms of the GNU General Public License as published by  *
 * the Free Software Foundation, either version 3 of the License, or     *
 * (at your option) any later version.                                   *
 *                                                                       *
 * This program is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 * GNU General Public License for more details.                          *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "gokey.h"


/* next tile index */
int next_tile;


Tile *
init_tiles (void)
{
  Tile *tiles;
  Tile empty_tile = { 0, 0, 0, 0, { 0, 0, 0, 0 }, 0 };
  int i, j, k, l = 0;
  tiles = malloc (TILE_MAX * sizeof (Tile));
  memcheck (tiles);
  for (i = 0; i < SIDE_MAX; i++)
    for (j = 0; j < COLOR_MAX; j++)
      for (k = 1; k < SIDE_TILE_MAX + 1; k++) {
        tiles[l] = empty_tile;
        tiles[l].order = k;
        tiles[l].color = j;
        l++;
      }
  /* last two tile is fake joker */
  for (; l < TILE_MAX; l++) {
    tiles[l] = empty_tile;
    tiles[l].type = FAKE_JOKER;
  }
  next_tile = 0;
  return tiles;
}

void
mix_tiles (void)
{
  int i, random = 0;
  Tile temp;
  for (i = 0; i < TILE_MAX; i++) {
    if (random == 0)
      srand (time (NULL));
    else
      srand (random);
    random = rand ();
    temp = tiles[i];
    tiles[i] = tiles[random % TILE_MAX];
    tiles[random % TILE_MAX] = temp;
  }
}

void
select_joker (void)
{
  int random = 0;
  do {
    if (random == 0)
      srand (time (NULL));
    else
      srand (random);
    joker = &tiles[random % TILE_MAX];
  } while (joker->type == FAKE_JOKER);
  joker->type = JOKER;
}

void
show_tiles (void)
{
  int i;
  for (i = 0; i < TILE_MAX; i++)
    printf ("%d %d\n", tiles[i].order, tiles[i].color);
}

void
show_player_hands (void)
{
  int i, j;
  Player *current = players;
  for (i = 0; i < PLAYER_MAX; i++) {
    printf ("%s:", current->name);
    for (j = 0; j < PLAYER_TILE_MAX; j++)
      if (current->tiles[j] != NULL)
        printf (" %d:%d", current->tiles[j]->order, current->tiles[j]->color);
    printf ("\n");
    current = current->next;
  }
}

Player *
init_players (const char **names)
{
  Player *player = NULL, *first;
  int i, j;
  for (i = 0; i < PLAYER_MAX; i++) {
    if (player == NULL) {
      player = malloc (PLAYER_MAX * sizeof (Player));
      memcheck (player);
    }

    /* setting variables */
    player->name = (char *) names[i];
    player->type = CPU;
    player->tiles = malloc (PLAYER_TILE_MAX * sizeof (Tile));
    memcheck (player->tiles);
    for (j = 0; j < PLAYER_TILE_MAX - 1; j++)
      player->tiles[j] = &tiles[next_tile++];
    /* last tile is NULL */
    player->tiles[j] = NULL;

    if (i == 0)
      first = player;

    if (i != PLAYER_MAX - 1) {
      player->next = malloc (PLAYER_MAX * sizeof (Player));
      memcheck (player->next);
      player = player->next;
    } else {
      player->next = first;
    }
  }
  player = first;
  return player;
}

void
init_game (void)
{
  const char *names[] = { "Player 1", "Player 2", "Player 3", "Player 4" };
  tiles = init_tiles ();
  players = init_players (names);
}
